﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Hook.Graphics;

namespace Hook.Primitives
{
    public class LineGrid
    {
        public List<Line> HorizontalLines, VerticalLines;
        public Vector2 Factor;

        public LineGrid()
        {
            HorizontalLines = new List<Line>();
            VerticalLines = new List<Line>();
            Factor = new Vector2(64, 64);
            RefreshLines();
        }

        public void SetFactor(Vector2 Factor)
        {
            this.Factor = Factor;
            RefreshLines();
        }

        public void RefreshLines()
        {
            HorizontalLines.Clear();
            HorizontalLines.Add(new Line(new PolyPoint(new Vector3(-Game1.RESOLUTION.X / 2, 0, 0)), new PolyPoint(new Vector3(Game1.RESOLUTION.X / 2, 0, 0))));
            for(float y = Factor.Y; y < Game1.RESOLUTION.Y / 2; y += Factor.Y)
            {
                HorizontalLines.Add(new Line(new PolyPoint(new Vector3(-Game1.RESOLUTION.X / 2, -y, 0)), new PolyPoint(new Vector3(Game1.RESOLUTION.X / 2, -y, 0))));
                HorizontalLines.Add(new Line(new PolyPoint(new Vector3(-Game1.RESOLUTION.X / 2, y, 0)), new PolyPoint(new Vector3(Game1.RESOLUTION.X / 2, y, 0))));
            }

            VerticalLines.Clear();
            VerticalLines.Add(new Line(new PolyPoint(new Vector3(0, -Game1.RESOLUTION.Y / 2, 0)), new PolyPoint(new Vector3(0, Game1.RESOLUTION.Y / 2, 0))));
            for(float x = Factor.X; x < Game1.RESOLUTION.X / 2; x += Factor.X)
            {
                VerticalLines.Add(new Line(new PolyPoint(new Vector3(-x, -Game1.RESOLUTION.Y / 2, 0)), new PolyPoint(new Vector3(-x, Game1.RESOLUTION.Y / 2, 0))));
                VerticalLines.Add(new Line(new PolyPoint(new Vector3(x, -Game1.RESOLUTION.Y / 2, 0)), new PolyPoint(new Vector3(x, Game1.RESOLUTION.Y / 2, 0))));
            }
        }

        public void Update(GameTime gt)
        {
            foreach(Line L in HorizontalLines)
            {
                L.Update(gt);
            }
            foreach(Line L in VerticalLines)
            {
                L.Update(gt);
            }
        }
        public void Draw(SpriteBatch sb, Camera Cam)
        {
            foreach(Line L in HorizontalLines)
            {
                L.Draw(sb, Cam);
            }
            foreach(Line L in VerticalLines)
            {
                L.Draw(sb, Cam);
            }
        }
    }
}
